package com.crp.unitalinforbaseplat.app.unitalinforbaseplat.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.crp.unitalinforbaseplat.app.core.BaseController;
import com.crp.unitalinforbaseplat.app.core.Message;
import com.crp.unitalinforbaseplat.app.core.MessageUtil;
import com.crp.unitalinforbaseplat.app.core.utils.EmptyUtils;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.*;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.service.IEnterStoreService;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.service.IPartsService;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 入库表 前端控制器
 * </p>
 *
 * @author zsx
 * @since 2020-04-20
 */
@RestController
@RequestMapping("/enterStore")
public class EnterStoreController extends BaseController {

    @Autowired
    IEnterStoreService enterStoreService;
    @Autowired
    IPartsService partsService;
    @Autowired
    IStoreService storeService;
    @RequestMapping("/list_index")
    ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("backend/unitalinforbaseplat/enter_store/list");
        modelAndView.addObject("roleName",getRoleName());
        return modelAndView;
    }
    @RequestMapping("/add_index")
    ModelAndView addIndex(EnterStore enterStore) {
        ModelAndView modelAndView = new ModelAndView("backend/unitalinforbaseplat/enter_store/add");
        modelAndView.addObject("roleName",getRoleName());
        if(!EmptyUtils.isEmpty(enterStore.getId())){
            modelAndView.addObject("enterStore",enterStoreService.getById(enterStore.getId()));
        }else{
            modelAndView.addObject("enterStore",enterStore);
        }
        return modelAndView;
    }

    @RequestMapping("/review_index")
    ModelAndView reviewIndex(EnterStore enterStore) {
        ModelAndView modelAndView = new ModelAndView("backend/unitalinforbaseplat/enter_store/review");
        modelAndView.addObject("roleName",getRoleName());
        if(!EmptyUtils.isEmpty(enterStore.getId())){
            modelAndView.addObject("enterStore",enterStoreService.getById(enterStore.getId()));
        }else{
            modelAndView.addObject("enterStore",enterStore);
        }
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/list")
    PageBean<EnterStore> index(EnterStore enterStore) {
        PageBean<EnterStore> pageBean = new PageBean<EnterStore>();
        List<EnterStore> storeList = enterStoreService.list(new LambdaQueryWrapper<EnterStore>().like(StringUtils.isNotBlank(enterStore.getName()),EnterStore::getName,enterStore.getName()));
        pageBean.setData(storeList);
        return pageBean;
    }

    @ResponseBody
    @RequestMapping("/listGroupByPartsId")
    PageBean<EnterStore> listGroupByPartsId(EnterStore enterStore) {
        PageBean<EnterStore> pageBean = new PageBean<EnterStore>();
        List<EnterStore> storeList = enterStoreService.listGroupByPartsId();
        pageBean.setData(storeList);
        return pageBean;
    }

    @ResponseBody
    @RequestMapping("/delete")
    Message delete(EnterStore enterStore) {
        enterStoreService.removeById(enterStore.getId());
        return MessageUtil.createMessage(true,"删除成功");
    }

    @ResponseBody
    @RequestMapping("/add")
    Message add(EnterStore enterStore) {
        Parts parts = partsService.getById(enterStore.getPartsId());
        Store store = storeService.getById(enterStore.getStoreId());
        enterStore.setName(parts.getName());
        enterStore.setRemainNum(enterStore.getNum());
        enterStore.setSpecifications(parts.getSpecifications());
        enterStore.setModel(parts.getModel());
        enterStore.setManufacturer(parts.getManufacturer());
        enterStore.setStoreName(store.getName());
        enterStore.setApplicationName(getUserName());
        enterStore.setCreateTime(new Date());
        enterStore.setEnterTime(new Date());
        enterStore.setCreateUserId(getId().intValue());
        enterStore.setStatus(1);
        if(EmptyUtils.isEmpty(enterStore.getId())){
            if(enterStoreService.save(enterStore)){
                return MessageUtil.createMessage(true,"新增成功");
            }else {
                return MessageUtil.createMessage(false,"新增失败");
            }
        }else {
            if(enterStoreService.updateById(enterStore)){
                return MessageUtil.createMessage(true,"修改成功");
            }else {
                return MessageUtil.createMessage(false,"修改失败");
            }
        }
    }

    @ResponseBody
    @RequestMapping("/review")
    Message review(@RequestParam("id")Integer id,@RequestParam("verifierOpinion")String verifierOpinion,@RequestParam("managersOpinion")String managersOpinion,@RequestParam("flag")Boolean flag) {
        EnterStore result = enterStoreService.getById(id);
        if(result.getStatus() == 1){
            result.setVerifierOpinion(verifierOpinion);
            if(!flag){
                result.setStatus(4);
                return MessageUtil.createMessage(true,"审核驳回成功");
            }
            result.setStatus(2);
            enterStoreService.updateById(result);
            return MessageUtil.createMessage(true,"校验员审核通过");
        }else if(result.getStatus() == 2){
            result.setManagersOpinion(managersOpinion);
            if(!flag){
                result.setStatus(4);
                return MessageUtil.createMessage(true,"审核驳回成功");
            }
            result.setStatus(3);
            enterStoreService.updateById(result);
            Parts parts = partsService.getById(result.getPartsId());
            parts.setNum(parts.getNum()+result.getNum());
            partsService.updateById(parts);
            return MessageUtil.createMessage(true,"经理审核通过");
        }else {
            return MessageUtil.createMessage(false,"非审核状态无法审核");
        }
    }
}
